package com.photoeditor.demo.ui.adapter.face;

import android.content.pm.PackageManager;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.photoeditor.R;
import com.photoeditor.demo.app.BaseRecycleViewAdapter;
import com.photoeditor.demo.imageloader.ImageLoader;
import com.photoeditor.demo.model.bean.SwapItemBean;

import butterknife.Bind;
import butterknife.ButterKnife;

public class FaceTemplateAdapter extends BaseRecycleViewAdapter<SwapItemBean> {

    private int mSelectedPosition = -1;

    @Override
    protected RecyclerView.ViewHolder createVHolder(ViewGroup parent, int viewType) {
        return new FaceTemplateViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_face_template, parent, false));
    }

    @Override
    protected void showItemView(RecyclerView.ViewHolder holder, int position) throws PackageManager.NameNotFoundException {
        FaceTemplateViewHolder vh = (FaceTemplateViewHolder) holder;
        SwapItemBean bean = mData.get(position);
        if (bean == null) {
            return;
        }
        ImageLoader.loadImage(bean.getUrl(), R.drawable.ic_default_image, vh.ivTemplate);
        if (mSelectedPosition == position) {
            vh.ivChosen.setVisibility(View.VISIBLE);
            vh.tvName.setTextColor(Color.parseColor("#FFCA0D"));
        } else {
            vh.ivChosen.setVisibility(View.GONE);
            vh.tvName.setTextColor(Color.parseColor("#B1B8E1"));
        }
        if (!TextUtils.isEmpty(bean.getName())) {
            vh.tvName.setText(bean.getName());
        }
    }

    @Override
    public long getAdapterItemId(int position) {
        return 0;
    }


    public void setSelectedPosition(int mSelectedPosition) {
        this.mSelectedPosition = mSelectedPosition;
        notifyDataSetChanged();
    }

    class FaceTemplateViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.iv_template)
        com.photoeditor.demo.ui.widget.RoundImageView ivTemplate;
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.iv_chosen)
        ImageView ivChosen;

        public FaceTemplateViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }
}
